// pages/app/schoolHome/schoolHome.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    schoolId: '',
    info: {}
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.setData({
      schoolId: options.schoolId
    })
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    var url = host + '/lx/mini/school/info';
    var params = {
      id: self.data.schoolId
    };
    var success = function (res) {
      var info = res.info;
      if (!wx.getStorageSync("user")) {
        info.subscribed = 0;
      }
      self.setData({
        info: info
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  subscribe(e) {
    if (!wx.getStorageSync("user")) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    var schoolId = self.data.info.id;
    var url = host + '/lx/mini/school/subscribe';
    var params = {
      schoolId: schoolId
    };
    var success = function (res) {
      wx.showToast({
        title: '已订阅',
        icon: 'success'
      })
      var info = self.data.info;
      info.subscribed = 1;
      self.setData({
        info: info
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  unsubscribe(e) {
    if (!wx.getStorageSync("user")) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    var schoolId = self.data.info.id;
    var url = host + '/lx/mini/school/unsubscribe';
    var params = {
      schoolId: schoolId
    };
    var success = function (res) {
      wx.showToast({
        title: '已取消订阅',
        icon: 'success'
      })
      var info = self.data.info;
      info.subscribed = 0;
      self.setData({
        info: info
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  goInfo(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/schools/schoolInfo/schoolInfo?schoolId=' + id
    })
  },
  goMajors(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/schools/majors/majors?schoolId=' + id
    })
  },
  goPolicies(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/schools/policy/policy?schoolId=' + id
    })
  },
  goQuestions(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/schools/questions/questions?schoolId=' + id
    })
  }
})